﻿using System;
using System.Windows.Forms;
using HZH_Controls;
using ShkSimulation.carsharing;
using ShkSimulation.core.template.model;

namespace ShkSimulation.desktop.setting.tools {
    public static class FormExtensions {

        public static void BindToEnumName(this ComboBox comboBox, Type enumType, int selectIndex = 0) {
            comboBox.DataSource = Enum.GetNames(enumType);
            comboBox.SelectedIndex = 0;
        }

        public static T GetSelectedItemToEnum<T>(this ComboBox comboBox) {
            return (T)Enum.Parse(typeof(T), comboBox.SelectedItem.ToString() ?? string.Empty, false);
        }

        public static void SetSelectedItemToEnum<T>(this ComboBox comboBox, T t) {
            comboBox.SelectedItem = t.ToString();
        }
        
        
        public static decimal TimeSpanToDayDecimal(this TimeSpan timeSpan) {
            return timeSpan.TotalDays.ToDecimal();
        }
        
        public static TimeSpan DayDecimalToTimeSpan(this decimal number) {
            return TimeSpan.FromDays(number.ToDouble());
        }
        
        public static decimal TimeSpanToSecondDecimal(this TimeSpan timeSpan) {
            return timeSpan.TotalSeconds.ToDecimal();
        }
        
        public static TimeSpan SecondDecimalToTimeSpan(this decimal number) {
            return TimeSpan.FromSeconds(number.ToDouble());
        }
        
        public static void ParseDecimalToTimeSpan(object sender, ConvertEventArgs e) {
            if (ConfigManager.UserRunConfig.真实时间) {
                e.Value = ((decimal) e.Value).SecondDecimalToTimeSpan();
            } else {
                e.Value = ((decimal) e.Value).DayDecimalToTimeSpan();
            }
        }
        
        public static void ParseTimeSpanToDecimal(object sender, ConvertEventArgs e) {
            if (ConfigManager.UserRunConfig.真实时间) {
                e.Value = ((TimeSpan) e.Value).TimeSpanToSecondDecimal();
            } else {
                e.Value = ((TimeSpan) e.Value).TimeSpanToDayDecimal();
            }
        }
        
        public static InputConfig Convert(this SimulationConfig simulationConfig) {
            InputConfig carsharingInput = new InputConfig();
            carsharingInput.SetResourceValue(CarsharingConfig.StationResourceKey, simulationConfig.站台文件);
            carsharingInput.SetResourceValue(CarsharingConfig.RoadResourceKey, simulationConfig.道路文件);
            carsharingInput.SetResourceValue(CarsharingConfig.DemandResourceKey, simulationConfig.需求文件);
            
            carsharingInput.SetConfigValue(CarsharingConfig.NavigateRouterPathKey, simulationConfig.导航文件);
            carsharingInput.SetConfigValue(CarsharingConfig.NavigateTypeKey, simulationConfig.导航类型);
            carsharingInput.SetConfigValue(CarsharingConfig.NavigateEnableKey, simulationConfig.启用导航);
            carsharingInput.SetConfigValue(CarsharingConfig.DispatchKey, simulationConfig.启用调度);
            carsharingInput.SetConfigValue(CarsharingConfig.DispatchUpBoundKey, simulationConfig.调度触发上阈值);
            carsharingInput.SetConfigValue(CarsharingConfig.DispatchDownBoundKey, simulationConfig.调度触发下阈值);
            return carsharingInput;
        }
                
        public static SimulationConfig Convert(this InputConfig carsharingInput) {
            SimulationConfig simulationConfig = new SimulationConfig();
            simulationConfig.站台文件 = carsharingInput.GetResourceValue(CarsharingConfig.StationResourceKey);
            simulationConfig.道路文件 = carsharingInput.GetResourceValue(CarsharingConfig.RoadResourceKey);
            simulationConfig.需求文件 = carsharingInput.GetResourceValue(CarsharingConfig.DemandResourceKey);
            
            simulationConfig.导航文件 = carsharingInput.GetConfigValue<string>(CarsharingConfig.NavigateRouterPathKey);
            simulationConfig.导航类型 = carsharingInput.GetConfigValue<NavigateType>(CarsharingConfig.NavigateTypeKey);
            simulationConfig.启用导航 = carsharingInput.GetConfigValue<bool>(CarsharingConfig.NavigateEnableKey);
            simulationConfig.启用调度 = carsharingInput.GetConfigValue<bool>(CarsharingConfig.DispatchKey);
            simulationConfig.调度触发上阈值 = carsharingInput.GetConfigValue<int>(CarsharingConfig.DispatchUpBoundKey);
            simulationConfig.调度触发下阈值 = carsharingInput.GetConfigValue<int>(CarsharingConfig.DispatchDownBoundKey);
            return simulationConfig;
        }

    }
}
